package com.ht.medicaltreatment.utils

import com.azhon.appupdate.manager.DownloadManager
import com.azhon.appupdate.util.ApkUtil
import com.blankj.utilcode.util.LogUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.pgyer.pgyersdk.PgyerSDKManager
import com.pgyer.pgyersdk.callback.CheckoutCallBack
import com.pgyer.pgyersdk.model.CheckSoftModel

object PgyUtils {
    fun update(activity: BaseActivity) {
        PgyerSDKManager.checkVersionUpdate(activity, object : CheckoutCallBack {
            override fun onNewVersionExist(model: CheckSoftModel) {
                ApkUtil.deleteOldApk(activity,"${activity.externalCacheDir?.path}/appupdate.apk")
                //检查版本成功（有新版本）
                val manager = DownloadManager.Builder(activity).run {
                    apkUrl(model.downloadURL)
                    apkName("appupdate.apk")
                    smallIcon(R.drawable.icon_logo)
                    //设置了此参数，那么内部会自动判断是否需要显示更新对话框，否则需要自己判断是否需要更新
                    apkVersionCode(model.buildVersionNo.toInt())
                    apkVersionName(model.buildVersion)
                    //强制更新
                    forcedUpgrade(model.isNeedForceUpdate)
                    apkDescription(model.buildUpdateDescription)
                    dialogImage(R.drawable.icon_update)
                    dialogButtonColor(activity.getColor(R.color.blue))
                    build()
                }
                manager.download()
            }

            override fun onNonentityVersionExist(error: String) {
                //ToastUtils.showShort("当前已是最新版本")
            }

            override fun onFail(error: String) {
                LogUtils.i(error)
            }
        })
    }
}